package com.example.wechathook.hook;

import static de.robv.android.xposed.XposedBridge.log;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;

import java.util.List;

import android.app.Activity;
import android.os.Handler;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

/**
 * 微信登录hook
 * 
 * @author chencq
 *
 */
public class LoginHook extends BaseHook {

	@Override
	public void hook(LoadPackageParam lpp) {
		findAndHookMethod("com.tencent.mm.ui.account.LoginUI", lpp.classLoader, "onResume", new XC_MethodHook() {
			@Override
			protected void afterHookedMethod(MethodHookParam param) throws Throwable {
				super.afterHookedMethod(param);
				if (param.thisObject instanceof Activity) {
					Activity activity = (Activity) (param.thisObject);
					final ViewGroup contentView = (ViewGroup) activity.getWindow().getDecorView();
					Handler handler = new Handler(activity.getMainLooper());

					List<EditText> editTexts = findEditText(contentView);
					if (editTexts.size() == 2) {
						final EditText etUserName = editTexts.get(0);
						final EditText etPassword = editTexts.get(1);

						handler.post(new Runnable() {
							@Override
							public void run() {
								etUserName.setText("2564927738");
							}
						});

						handler.post(new Runnable() {
							@Override
							public void run() {
								etPassword.setText("wo@1314520");
							}
						});

						log("登录：" + etUserName.getId() + "," + etPassword.getId());

						handler.post(new Runnable() {
							@Override
							public void run() {
								// 找到登录按钮，然后单击登录
								Button btnLogin = findButton(contentView, "登录");
								if (btnLogin != null) {
									btnLogin.performClick();
								}
								log("登录button：" + btnLogin.getId());
							}
						});

					}

				}
			}
		});
	}

}
